/**
 * @Author       : Mo 2427995270@qq.com
 * @Date         : 2025-06-17 17:25:33
 * @LastEditTime : 2025-08-19 22:09:23
 * @FilePath     : \hal_driver\ll_e_signature.c
 * @Description  : 电子签名低层驱动
 * @Version      : V1.0
 * @History      :
 * @Note         :
 */
/***************************************Includes***********************************/
#include "ll_e_signature.h"
#include "ll_flash.h"
#include "xxtea.h"

#if defined(GD32F407)
#include "gd32f4xx.h"
#elif defined(STM32F40_41xxx)
#include "stm32f4xx.h"
#elif defined(GD32F10X_HD)
#include "gd32f10x.h"
#elif defined(STM32F10X_HD)
#include "stm32f10x.h"
#elif defined(PY32F071xB)
#include "py32f0xx.h"
#elif defined(STM32F0XX) || defined(STM32F030x6)
#include "stm32f0xx.h"
#else
#error "No device selected"
#endif

/************************************Private Macros********************************/
// #define


/**********************************Private Statement*******************************/


/***********************************Public Variables*******************************/

#if defined(GD32F407)
static const uint32_t (*e_signature_id)[3] = (void *)0x1fff7a10U;
#elif defined(STM32F40_41xxx)
static const uint32_t (*e_signature_id)[3] = (void *)0x1fff7a10U;
#elif defined(GD32F10X_HD)
static const uint32_t (*e_signature_id)[3] = (void *)0x1ffff7e8U;
#elif defined(STM32F10X_HD)
static const uint32_t (*e_signature_id)[3] = (void *)0x1ffff7e8U;
#elif defined(PY32F071xB)
static const uint32_t (*e_signature_id)[3] = (void *)0x1fff3000U;
#elif defined(STM32F0XX) || defined(STM32F030x6)
static const uint32_t (*e_signature_id)[3] = (void *)0x1ffff7acU;
#else
#error "No device selected"
#endif


static const volatile uint32_t e_signature_key[3] = {0xffffffff, 0xffffffff, 0xffffffff};
static const volatile uint32_t key[4] = {0x98765432, 0x87654321, 0x34567890, 0x56181405};
/**********************************Private Variables*******************************/



/**********************************Public Functions********************************/

/**
 * @brief 设置设备密钥
 * @return int 0设置成功 已设置
 * @note null
 */
int e_signature_set_key(void) {
    if ((e_signature_key[0] == 0xffffffff) && (e_signature_key[1] == 0xffffffff) && (e_signature_key[2] == 0xffffffff)) {
        // 密钥未设置
        uint32_t secret_key[3] = {(*e_signature_id)[0], (*e_signature_id)[1], (*e_signature_id)[2]};
        xxtea(secret_key, 3, (const uint32_t *)key);
        // 使用不擦除版本，尽可在第一次上电时写入密钥
        flash_ll_write_no_erase((uint32_t)&e_signature_key, secret_key, sizeof(secret_key));
        return 0;
    }
    // 密钥已设置
    return -1;
}
/**
 * @brief 验证设备密钥
 * @return int 0正确 -1失败
 * @note null
 */
int e_signature_verify_key(void) {
    uint32_t secret_key[3] = {(*e_signature_id)[0], (*e_signature_id)[1], (*e_signature_id)[2]};
    xxtea(secret_key, 3, (const uint32_t *)key);
    if ((e_signature_key[0] == secret_key[0]) && (e_signature_key[1] == secret_key[1]) && (e_signature_key[2] == secret_key[2])) {
        return 0;
    }
    return -1;
}

/**********************************Private Functions*******************************/


/* [] END OF FILE */
